package com.lv.leyou.web;

import com.lv.leyou.common.dto.PageDTO;
import com.lv.leyou.dto.BrandDTO;
import com.lv.leyou.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    //根据品牌id查询品牌
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.brandService.queryBrandById(id));
    }

    //根据品牌id集合查询品牌集合
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(BrandDTO.convertEntityList(this.brandService.queryBrandByIds(ids)));
    }

    //分页查询品牌
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> queryPage(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "rows", defaultValue = "5") Integer rows, @RequestParam(value = "key", required = false) String key) {

        return ResponseEntity.ok(this.brandService.queryPage(page, rows, key));
    }

    //根据分类id查询品牌
    @GetMapping("of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategoryId(@RequestParam("id") Integer id) {
        return ResponseEntity.ok(this.brandService.queryBrandByCategoryId(id));
    }

    //新增品牌
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO) {
        brandService.saveBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //更新品牌
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO) {
        brandService.updateBrand(brandDTO);
        return ResponseEntity.status(201).build();
    }

    //根据id删除品牌
    @DeleteMapping("{id}")
    public ResponseEntity<Void> deleteByBrandId(@PathVariable("id") Integer id) {
        brandService.deleteByBrandId(id);
        return ResponseEntity.status(200).build();
    }
}
